const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});

//==============1最新动态==========
//业务逻辑
module.exports = function() {
    var router = express.Router();
    //查询最新动态
    router.get('/PrimaryClassification.html',function(req,res,next){
        var delelt= + req.query.forumAId;

                //格式化url栏的参数，转换为对像
                //用户确定要删除，查询当前对应的是否有商品,没有商品就删除成功
                if(urlobj.parse(req.url,true).query.act==null||urlobj.parse(req.url,true).query.act=="")
                {
                    db.query('SELECT * FROM forumainfo',function(err,data){
                        if(err){
                            res.status(400).send('<script>alert("数据查询失败，请检查后再次查询!");window.location.href="404.html";</script>').end();
                        }
                        else{
                            if(data.length==0){
                                res.status(400).send('<script>alert("暂无可用数据!");window.location.href="404.html";</script>').end();
                            }
                            else{
                                req.msg=data;

                            }
                            next();
                        }
                    });
                }
                else if(urlobj.parse(req.url,true).query.act=="delete")
                {
                    var delelt= + req.query.forumAId;
                     db.query("SELECT * FROM forumainfo,forumbinfo WHERE forumbinfo.forumAId=forumainfo.forumAId and forumainfo.forumAId="+ parseInt(delelt),function(err,data) {
                            if(err)
                         {
                             res.send('<script>alert("出错了!");window.location.href="404.html";</script>').end();
                         }else
                            {

                                if(data.length==0)
                                {
                                    console.log(5);
                                    db.query("DELETE FROM forumainfo WHERE forumAId="+parseInt(req.query.forumAId),function(err,data) {
                                        if(err)
                                        {
                                            res.status(500).send('<script>alert("该分类下游数据!");window.location.href="PrimaryClassification.html";</script>').end();
                                        }else
                                        {
                                            res.status(200).send('<script>alert("删除成功!");window.location.href="PrimaryClassification.html";</script>').end();
                                        }
                                    });

                                }else{
                                     res.send('<script>alert("该分类下游数据!");window.location.href="PrimaryClassification.html";</script>').end();
                                }
                            }
                     });
                }
    });
    //执行删除事件
    // router.get('/PrimaryClassification.html',function(req,res){
    //     //     //删除一级分类信息
    //     //
    //     // });

   //开始渲染后台首页
    router.use('/PrimaryClassification.html',function(req,res,next){
        res.render("forum/PrimaryClassification.ejs",{"msg":req.msg});
    });
    return router;
}